﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_EditUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string member_ID = Request.QueryString["User_ID"];
           

            
                LoadControlForEdit(member_ID);

        }
        
    }
    #region For Set control when edit/insert
    private void SetControlForInsert()
    {
        btnAdd.Visible = true;
        btnEdit.Visible = false;
        lblMK.Text = "Mật khẩu :";
        lblMK.Visible = true;
        txtMK.Visible = true;
        btnBack.Attributes.Add("onclick", "javascript :history.back(1)");
    }

    private void LoadControlForEdit(string member_ID)
    {
        User user = User_Session.Get_User_ByID(int.Parse(member_ID));
        if (user != null)
        {
            //Gan control voi cac truong trong doi tuong
            txtAddress.Value = user.User_Address;
            //txtBirthday.Text = user.User_Birthday.ToString();
            txtCompany.Value = user.User_Company;
            txtEmail.Value = user.User_Email;
            txtUser_Name.Value = user.User_Name;
            txtNick.Value = user.User_Nick;
            rdiNam.Checked = user.User_Sex;
            rdiNu.Checked = !user.User_Sex;
            ddlGroup.SelectedIndex = (int)user.User_Type;
            txtPhone.Value = user.User_Phone;

        }
        btnAdd.Visible = false;
        btnEdit.Visible = true;
        lblMK.Visible = false;
        txtMK.Visible = false;
    }
    #endregion
    #region Insert/Update and upload image
    protected void Update()
    {
        string member_ID = Request.QueryString["User_ID"];
        User user = User_Session.Get_User_ByID(int.Parse(member_ID));
        if (user != null)
        {
//            WebMsgBox.Show(txtUser_Name.Value);
            user.User_Address = txtAddress.Value;

            user.User_Company = txtCompany.Value;
            user.User_Email = txtEmail.Value;
            user.User_Name = txtUser_Name.Value;
            user.User_Nick = txtNick.Value;

            user.User_Sex = rdiNam.Checked;
            user.User_Type = byte.Parse(ddlGroup.SelectedValue.ToString());
            user.User_Phone = txtPhone.Value;
            User_Session.Update_User(user);

        }

    }
    protected void Insert()
    {
        User user = new User();
        //Gan du lieu vao doi tuong		
        user.User_Address = txtAddress.Value;
        //user.User_Birthday = Convert.ToDateTime(txtBirthday.Text);
        user.User_Company = txtCompany.Value;
        user.User_Email = txtEmail.Value;
        user.User_Name = txtUser_Name.Value;
        user.User_Nick = txtNick.Value;
        user.User_Sex = rdiNam.Checked;
        user.User_Type = byte.Parse(ddlGroup.SelectedValue.ToString());
        user.User_Phone = txtPhone.Value;
        //user.User_CreateDate = DateTime.Now;
        //user.User_Password = CryptorEngine.Encrypt(txtMK.Text.Trim(), true);
        //Them moi
        User_Session.Insert_User(user);
        Response.Redirect("ManageUser.aspx");
    }


    #endregion
    #region Button event
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Insert();
            WebMsgBox.Show("Đã thêm mới thành công.");
        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Thêm mới không thành công do có lỗi xảy ra.");
            throw ex;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Update();
            WebMsgBox.Show("Đã cập nhật thành công.");
        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Cập nhật không thành công do có lỗi xảy ra.");
            throw ex;
        }
    }
    #endregion
    protected void btnAcceptChange_Click(object sender, EventArgs e)
    {
        if (txtPass.Text.Length <= 0 || txtPass2.Text.Length <= 0)
        {
            WebMsgBox.Show("Password không được rỗng.");
            txtPass.Focus();
            return;
        }
        if (txtPass.Text.Trim() != txtPass2.Text.Trim())
        {
            WebMsgBox.Show("Password không trùng nhau.");
            txtPass.Focus();
            return;
        }
        string member_ID = Request.QueryString["User_ID"];

        User user = User_Session.Get_User_ByID(int.Parse(member_ID));
        if (user != null)
        {

            //user.User_Password = CryptorEngine.Encrypt(txtPass.Text.Trim(), true);
            User_Session.Update_User(user);
            WebMsgBox.Show("Password đã được thay đổi.");
        }
    }
    protected void btnBack_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("ManageMember.aspx");
    }
}
